#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QLineEdit>
#include <QString>
#include <QMessageBox>
#include <QRadioButton>
#include <QImage>
#include <QLabel>
#include<QTextCodec>
#include "imgpro.h"
#include"colorwheel.h"
#include"recoloring.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->scrollArea->setBackgroundRole(QPalette::Dark);
    colorwheel=new ColorWheel();
    //大作业
    ui->wheelLayout->addWidget(colorwheel);
    for(int i=0;i<5;i++)
    {
        colorblock[i]=new ColorBlock(0,i,60);
        ui->cbLayout->addWidget(colorblock[i]);
        connect(this->colorblock[i],SIGNAL(sendId(int)),this,SLOT(on_select_colorblock(int)));
    }
    this->recoloring=new Recoloring();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_open_clicked()
{
     QString strFileName = QFileDialog::getOpenFileName(this,tr("select"),".",tr("open (*.png *.jpg *.jpeg *.bmp)"));
     //解决中文路径名问题
     QTextCodec *code = QTextCodec::codecForName("GB2312");
     std::string newName = code->fromUnicode(strFileName).data();

     //初始化
     if(newName.length()==0)
         return;
     image = cv::imread(newName);
     imaging = cv::imread(newName);
     image.copyTo(imagewp->img);
     image.copyTo(imagewp->dst);
     imagewp->radius = 30;

     //显示图像
     cv::Mat imgshow;
     cv::cvtColor(image,imgshow,CV_BGR2RGB);
     QImage img= QImage((const unsigned char*)(imgshow.data),
               imgshow.cols,imgshow.rows,(int)imgshow.step,QImage::Format_RGB888);
     img.bits();
     ui->canvas->setPixmap(QPixmap::fromImage(img));
     ui->scrollArea->setWidget(ui->canvas);


     //大作业
     //添加调色板颜色
     this->recoloring->init(this->image);
     for(int i=0;i<5;++i)
     {
         disconnect(this->colorblock[i],SIGNAL(sendColorAndId(const QColor&,int)),
                     this->recoloring,SLOT(Shit(const QColor&,int)));
         this->colorblock[i]->setColor(QColor(this->recoloring->center[3*i]*255,
                                       this->recoloring->center[3*i+1]*255,
                                        this->recoloring->center[3*i+2]*255));
         this->colorblock[i]->setFocused(false);
         connect(this->colorblock[i],SIGNAL(sendColorAndId(const QColor&,int)),
                     this->recoloring,SLOT(Shit(const QColor&,int)));
     }
     connect(this->recoloring,SIGNAL(sendShit(Mat)),this,SLOT(on_receive_shit(Mat)));


     //使得各个控件有效
     ui->save->setEnabled(true);
     ui->MedianRadioButton->setEnabled(true);
     ui->GaussianRadioButton->setEnabled(true);
     ui->SliderControl->setEnabled(true);
     ui->MeanRadioButton->setEnabled(true);
     ui->saturationRadioButton->setEnabled(true);
     ui->SharpButton->setEnabled(true);
     ui->liqueRadioButton->setEnabled(true);
     ui->BilaRadioButton->setEnabled(true);
     ui->SmartBilaRadioButton->setEnabled(true);
     ui->lomoRadioButton->setEnabled(true);
     ui->whiteRadioButton->setEnabled(true);
}

void MainWindow::on_save_clicked()
{

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               "/home/jana/lena.png",
                               tr("Images (*.png *.bmp *.jpg)"));
    QTextCodec *code = QTextCodec::codecForName("GB2312");
    std::string name = code->fromUnicode(fileName).data();
    if(name.length()==0)
        return;
    cv::Mat imgsave;
    cv::cvtColor(imaging,imgsave,CV_BGR2RGB);
    cv::imwrite(name,imgsave);
}

void MainWindow::on_SliderControl_sliderReleased()
{
    int value=ui->SliderControl->sliderPosition();
    switch(filters)
    {
    case Median:
        MedianFilter(image,imaging,value*2+1);
        break;
    case Gaussian:
        Filter(image,imaging,value*2+1);
        break;
    case Mean:
        Filter(image,imaging,value*2+1);
        break;
    case Saturation:
        SaturateProc(image,imaging,(value-5)*45);
        break;
    case Lique:
        imagewp->radius=value*15;
        imagewp->dst.copyTo(imaging);
        break;
    case Bila:
        _BilateralFilter(image, imaging,25 ,40,value*4+1 );
        break;
    case SmartBila:
        BilateralFilter(image, imaging,25 ,40,value*4+1 );
        break;
    case Lomo:
        LomoFilter(image,imaging,value*20);
        break;
    case White:
        WhiteShit(image,imaging,150-value*10);
        break;
    }
    showImage(imaging);
}

void MainWindow::on_MedianRadioButton_clicked()
{
    filters=Median;
}

void MainWindow::on_GaussianRadioButton_clicked()
{
    filters=Gaussian;
}

void MainWindow::on_MeanRadioButton_clicked()
{
    filters=Mean;
}

void MainWindow::on_saturationRadioButton_clicked()
{
    filters=Saturation;
}

void MainWindow::on_liqueRadioButton_clicked()
{
    filters=Lique;
    ui->liqButton->setEnabled(true);
}

void MainWindow::on_liqueRadioButton_toggled(bool checked)
{
    if(!checked)
        ui->liqButton->setEnabled(false);
}

void MainWindow::on_BilaRadioButton_clicked()
{
    filters=Bila;
}
void MainWindow::on_SmartBilaRadioButton_clicked()
{
    filters=SmartBila;
}
void MainWindow::on_lomoRadioButton_clicked()
{
    filters=Lomo;

}

void MainWindow::on_whiteRadioButton_clicked()
{
    filters=White;
}

void MainWindow::showImage(const Mat a)
{
    cv::cvtColor(a,a,CV_RGB2BGR);
    QImage img= QImage((const unsigned char*)(a.data),
              a.cols,a.rows,(int)a.step,QImage::Format_RGB888);
    img.bits();
    ui->canvas->setPixmap(QPixmap::fromImage(img));
}

void MainWindow::on_SharpButton_clicked()
{
    Mat temp,process;
    image.copyTo(temp);
    image.copyTo(process);
    Filter(temp,temp,3);
    sharpen(temp,process);
    showImage(process);
}


void MainWindow::on_liqButton_pressed()
{
    int originx=(ui->scrollArea->width()-image.cols)/2;
    int originy=(ui->scrollArea->height()-image.rows)/2;
    if(originx<0)
        originx=0;
    if(originy<0)
        originy=0;
    imagewp->origCenterX = ui->canvas->mapFromGlobal(QCursor::pos()).x()-originx;
    imagewp->origCenterY = ui->canvas->mapFromGlobal(QCursor::pos()).y()-originy;
}

void MainWindow::on_liqButton_released()
{
    int originx=(ui->scrollArea->width()-image.cols)/2;
    int originy=(ui->scrollArea->height()-image.rows)/2;
    if(originx<0)
        originx=0;
    if(originy<0)
        originy=0;
    imagewp->nowCenterX = ui->canvas->mapFromGlobal(QCursor::pos()).x()-originx;
    imagewp->nowCenterY = ui->canvas->mapFromGlobal(QCursor::pos()).y()-originy;
    imageWarp(this->imagewp);
    imagewp->img.copyTo(imaging);
    showImage(imaging);
}

void MainWindow::on_select_colorblock(int id)
{
    disconnect(this->colorwheel,SIGNAL(colorChange(QColor)),this->colorblock[cb_old_id],SLOT(setColor(QColor)));
    this->colorblock[cb_old_id]->setFocused(false);
    this->colorblock[id]->setFocused(true);
    this->colorwheel->setColor(this->colorblock[id]->color);
    connect(this->colorwheel,SIGNAL(colorChange(QColor)),this->colorblock[id],SLOT(setColor(QColor)));
    cb_old_id=id;
}

void MainWindow::on_receive_shit(Mat shit)
{
    //cv::cvtColor(shit,shit,CV_RGB2BGR);
    QImage img= QImage((const unsigned char*)(shit.data),
              shit.cols,shit.rows,(int)shit.step,QImage::Format_RGB888);
    img.bits();
    ui->canvas->setPixmap(QPixmap::fromImage(img));
    //qDebug("没事执行个鬼");
}



